package com.cadre.web.controller.officer;

import com.cadre.common.utils.R;
import com.cadre.common.utils.common.GenerateUUID;
import com.cadre.system.domain.officer.A01;
import com.cadre.system.domain.officer.A27;
import com.cadre.system.service.A01Service;
import com.cadre.system.service.A27Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping({"/profess/a27"})
@RestController
public class A27Controller {
    @Resource
    private A27Service a27Service;
    @Resource
    private A01Service a01Service;

    @GetMapping({"/list"})
    public R list(String personid) {
        Map<String, Object> map = new HashMap<>();
        map.put("personid", personid);
        return R.ok().put("data", (Object) this.a27Service.selectByList(map));
    }

    @GetMapping({"/info/{flowId}"})
    public R info(@PathVariable("flowId") String flowId) {
        return R.ok().put("a27", (Object) ((A27) this.a27Service.getBaseMapper().selectById(flowId)));
    }

    @PostMapping({"/save"})
    public R save(@RequestBody A27 a27) {
        a27.setFlowid(GenerateUUID.getUUID());
        a27.setIsout((short) 0);
        this.a27Service.insertSelective(a27);
        return R.ok();
    }

    @PostMapping({"/update"})
    public R update(@RequestBody A27 a27) {
        this.a27Service.updateByPrimaryKeySelective(a27);
        return R.ok();
    }

    @PostMapping({"/saveA2701"})
    public R saveA0195(@RequestBody A27[] a27) {
        String att = "";
        if (a27.length <= 0) {
            return R.error();
        }
        for (int i = 0; i < a27.length; i++) {
            this.a27Service.updateByPrimaryKeySelective(a27[i]);
            att = att + a27[i].getA2715() + "经" + a27[i].getA2720() + "批准," + a27[i].getA2703() + ";";
        }
        String att2 = att.substring(0, att.length() - 1);
        A01 a01 = new A01();
        a01.setA2701(att2);
        a01.setFlowid(a27[0].getPersonid());
        if (this.a01Service.updateByPrimaryKeySelective(a01) == 1) {
            return R.ok().put("data", (Object) att2);
        }
        return R.error();
    }

    @PostMapping({"/delete"})
    public R delete(@RequestBody String[] flowids) {
        this.a27Service.getBaseMapper().deleteBatchIds(Arrays.asList(flowids));
        return R.ok();
    }

    @PostMapping({"/updateList"})
    public R updateList(@RequestBody List<A27> entityList, @RequestParam String personid) {
        if (entityList.size() > 0) {
            if (!(personid == null || personid == "")) {
                Map<String, Object> map = new HashMap<>();
                map.put("personid", personid);
                List<A27> list = this.a27Service.selectByList(map);
                if (list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).setIsout((short) 0);
                    }
                    this.a27Service.updateBatchById(list);
                }
            }
            this.a27Service.updateBatchById(entityList);
            return R.ok();
        }
        if (!(personid == null || personid == "")) {
            Map<String, Object> map2 = new HashMap<>();
            map2.put("personid", personid);
            List<A27> list2 = this.a27Service.selectByList(map2);
            if (list2.size() > 0) {
                for (int i2 = 0; i2 < list2.size(); i2++) {
                    list2.get(i2).setIsout((short) 0);
                }
                this.a27Service.updateBatchById(list2);
            }
        }
        return R.ok();
    }
}
